#!/usr/bin/env bash
# calibration_plot -- Produces calibration plot for an application variable
# $ calibration_plot VARIABLE
# Produces files VARIABLE.tsv and VARIABLE.png on the current working directory.
##
set -euo pipefail

[[ $# -gt 0 ]] || usage "$0" "VARIABLE missing"
Variable=$1; shift

calibration_view="$Variable"_calibration
tsv="$Variable".tsv
png="$Variable".png

# first, get the data in tsv from the database
deepdive sql eval "
    SELECT probability_lo
         , probability_hi
         , num_variables
         , num_correct
         , num_incorrect
      FROM $calibration_view
" format=tsv >"$tsv"

# then, produce the calibration plot
# as PNG image with gnuplot
gnuplot -e "input_file=\"$tsv\";output_file=\"$png\"" "$DEEPDIVE_HOME"/util/calibration.plg ||
    # or try Python with Matplotlib if that fails
    calibration.py "$tsv" "$png" ||
    # Matplotlib has issues with virtualenv, so try with system python if that fails
    /usr/bin/python "$DEEPDIVE_HOME"/util/calibration.py "$tsv" "$png" ||
    true
